package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.enums.AuditTypeEnum;
import com.useeinfo.oa.modules.inspection.entity.InsAuditOrder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("InsAuditOrderDao")
public class InsAuditOrderDao implements CrudDao<InsAuditOrder> {
    private final static Logger logger = LoggerFactory.getLogger(InsAuditOrderDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("i.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String inspectionOrderId = queryHash.get("inspectionOrderId");
        if (!StringUtils.isEmpty(inspectionOrderId)) {
            conditionHash.put("i.inspectionOrder.id = ?{paramIndex} ", StringConverters.null2Long(inspectionOrderId));
        }
        String name = queryHash.get("name");
        if (!StringUtils.isEmpty(name)) {
            conditionHash.put("(i.inspectionOrder.orderName like ?{paramIndex}) ", "%"+name+"%");
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(i.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(i.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(i.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String auditUserId = queryHash.get("auditUserId");
        if (!StringUtils.isEmpty(auditUserId)) {
            conditionHash.put("(i.auditUser.id = ?{paramIndex}) ", StringConverters.null2Long(auditUserId));
        }
       String auditType = queryHash.get("auditType");
        if (StringUtils.isNotBlank(auditType)) {
            String[] statusArray = auditType.split(",");
            for (int i = 0; i < statusArray.length; i++) {
                if (i == 0) {
                    String k = statusArray.length == 1 ? "auditTypeEnum = ?{paramIndex} " : "(auditTypeEnum = ?{paramIndex} ";
                    conditionHash.put(k, AuditTypeEnum.valueOf(statusArray[i].trim()));
                } else if (i == statusArray.length - 1) {
                    conditionHash.put("or auditTypeEnum = ?{paramIndex}) ", AuditTypeEnum.valueOf(statusArray[i].trim()));
                } else {
                    String k = String.format("or auditTypeEnum = ?{paramIndex} %" + i + "s", "");
                    conditionHash.put(k, AuditTypeEnum.valueOf(statusArray[i].trim()));
                }
            }
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(i) from InsAuditOrder i ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InsAuditOrder> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select i from InsAuditOrder i ", "order by i.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InsAuditOrder> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InsAuditOrder.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InsAuditOrder findModel(Long id) {
        return entityManager.find(InsAuditOrder.class, id);
    }

    @Override
    public Integer add(InsAuditOrder model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InsAuditOrderImpl添加InsAuditOrder成功！");
        return 1;
    }

    @Override
    public Integer update(InsAuditOrder model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InsAuditOrder insAuditOrder = entityManager.find(InsAuditOrder.class, id);
        entityManager.remove(insAuditOrder);
        return 1;
    }

    @Override
    public Integer deleteLogic(InsAuditOrder model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
